<?php
// +----------------------------
// | Created by PhpStorm
// +----------------------------
// | Author: YnnSon
// +----------------------------
namespace app\api\controller\v1;

use app\api\model\Company as CompanyModel;
use app\api\validate\ParameterMustBePositiveInteger;
use think\Request;

/**
 * 检车站
 * Class Company
 * @package app\api\controller\v1
 */
class Company extends BaseController
{
    /**
     * @Desc 获取检车站列表
     * @return array
     */
    public function getList()
    {
        $param = Request::instance()->param();
        $list = CompanyModel::getList($param);
        foreach ($list as $key => $value) {
            // 经纬度为0代表地理位置未授权
            if (0 != $param['userLng'] && 0 != $param['userLat']) {
                // 计算两地距离(单位km)
                $distance = getDistance($param['userLat'], $param['userLng'], $value['latitude'], $value['longitude']);
                $list[$key]['distance'] = $distance;
            } else {
                $list[$key]['distance'] = 0;
            }
        }
        //根据字段last_name对数组$data进行降序排列
        $distance = array_column($list, 'distance');
        array_multisort($distance, $list);
        return $list;
    }

    /**
     * @Desc 获取检车站详情
     * @return array
     */
    public function getDetail($id)
    {
        (new ParameterMustBePositiveInteger())->scene('id')->goCheck();
        return CompanyModel::getDetail($id);
    }

    /**
     * @Desc 收银系统 - 获取检车站列表
     * @return array
     */
    public function getListToPayment()
    {
        $data = CompanyModel::getListToPayment();
        return [
            'code' => 1,
            'data' => $data,
        ];
    }
    /**
     * @Desc 获取检车站列表
     * @return array
     */
    public function getList2()
    {
        $param = Request::instance()->param();
        $list = CompanyModel::getList2($param);
        // 经纬度为0代表地理位置未授权
        if (0 != $param['userLng'] && 0 != $param['userLat']) {
            foreach ($list as $key => $value) {
                // 计算两地距离(单位km)
                $distance = getDistance($param['userLat'], $param['userLng'], $value['latitude'], $value['longitude']);
                $list[$key]['distance'] = $distance;
                if (0 != $param['distanceId'] && (number_format($param['distanceId'], 2) < $distance)) {
                    unset($list[$key]);
                }
            }
        }
        //根据字段last_name对数组$data进行降序排列
        $distance = array_column($list, 'distance');
        !empty($distance) && array_multisort($distance, $list);
        $count = count($list);
        return [
            'count'  => $count,
            'list'   => $list,
        ];
    }
}